﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.Common;
using MySql.Data.Types;
using MySql.Data.MySqlClient;
using MySql.Data;

namespace MySqlCodeGeneratorLib
{
    public class MySqlInformation
    {
        private MySqlConnection connection;
        private const string SQL_DBLIST_COMMAND = "SELECT SCHEMA_NAME FROM SCHEMATA";

        public List<string> Login(string server, string username, string password, string port)
        {
            string connectionstring = "";
            List<string> dblst = new List<string>();

            try
            {
                connectionstring = String.Format("Server={0}; Database=INFORMATION_SCHEMA;Uid={1};Pwd={2};Port={3};", server, username, password,port);
                connection = new MySqlConnection(connectionstring);
                connection.Open();
                MySqlCommand cmd = new MySqlCommand(SQL_DBLIST_COMMAND,connection);
                MySqlDataReader dr = cmd.ExecuteReader();

                if (dr.HasRows)
                    while (dr.Read())
                    {
                        if(dr[0] != DBNull.Value && 
                            (dr[0].ToString().ToLower() != "mysql" && dr[0].ToString().ToLower() !="information_schema"))
                        {
                            dblst.Add(dr[0].ToString());
                        }
                    }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (connection != null && connection.State != System.Data.ConnectionState.Closed)
                    connection.Close();
            }
           

            return dblst;
        }
    }
}
